package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.base.entity.TruckDriver;
import com.guigu.base.entity.TruckDriverLicense;
import com.guigu.base.entity.dto.TruckDriverDto;
import com.guigu.base.entity.dto.TruckDriverLicenseDto;
import com.guigu.base.service.TruckDriverLicenseService;
import com.guigu.base.service.TruckDriverService;
import com.guigu.common.utils.PageResponse;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 司机表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/goods/pd-truck-driver")
@Api(tags = "司机管理")
public class TruckDriverController {
    @Autowired
    private TruckDriverService truckDriverService;
    @Autowired
    private TruckDriverLicenseService truckDriverLicenseService;

    /**
     * 保存司机基本信息
     *
     * @param dto 司机基本信息
     * @return 返回信息
     */
    @PostMapping("")
    @ApiOperation("保存司机基本信息")
    public R<TruckDriverDto> saveDriver(@RequestBody TruckDriverDto dto) {
        TruckDriver driver = new TruckDriver();
        BeanUtils.copyProperties(dto, driver);
        truckDriverService.saveTruckDriver(driver);
        BeanUtils.copyProperties(driver, dto);
        return new R<>("2000", dto);
    }

    /**
     * 获取司机基本信息列表
     *
     * @param userIds 司机id列表
     * @return 司机基本信息列表
     */
    @GetMapping("")
    @ApiOperation("获取司机基本信息列表")
    public R<List<TruckDriverDto>> findAllDriver(@RequestParam(name = "userIds", required = false) List<String> userIds, @RequestParam(name = "fleetId", required = false) String fleetId) {
        List<TruckDriverDto> list = truckDriverService.findAll(userIds, fleetId).stream().map(pdTruckDriver -> {
            TruckDriverDto dto = new TruckDriverDto();
            BeanUtils.copyProperties(pdTruckDriver, dto);
            return dto;
        }).collect(Collectors.toList());
        return new R<>(ResultCode.SUCCESS_CODE, list);
    }

    /**
     * 获取司机基本信息
     *
     * @param id 司机id
     * @return 司机基本信息
     */
    @GetMapping("/{id}")
    @ApiOperation("获取司机基本信息")
    public R<TruckDriverDto> findOneDriver(@PathVariable(name = "id") String id) {
        TruckDriver pdTruckDriver = truckDriverService.findOne(id);
        TruckDriverDto dto = new TruckDriverDto();
        if (ObjectUtils.isEmpty(dto)) {
            return new R<>("2000", null);
        }
        if (pdTruckDriver != null) {
            BeanUtils.copyProperties(pdTruckDriver, dto);
        }
        return new R<>(ResultCode.SUCCESS_CODE, dto);
    }

    /**
     * 保存司机驾驶证信息
     *
     * @param dto 司机驾驶证信息
     * @return 返回信息
     */
    @PostMapping("/driverLicense")
    @ApiOperation("保存司机驾驶证信息")
    public R<TruckDriverLicenseDto> saveDriverLicense(@RequestBody TruckDriverLicenseDto dto) {
        TruckDriverLicense driverLicense = new TruckDriverLicense();
        BeanUtils.copyProperties(dto, driverLicense);
        truckDriverLicenseService.saveTruckDriverLicense(driverLicense);
        BeanUtils.copyProperties(driverLicense, dto);
        return new R<>(ResultCode.SUCCESS_CODE, dto);
    }

    /**
     * 获取司机驾驶证信息
     *
     * @param id 司机id
     * @return 司机驾驶证信息
     */
    @GetMapping("/{id}/driverLicense")
    @ApiOperation("获取司机驾驶证信息")
    public R<TruckDriverLicenseDto> findOneDriverLicense(@PathVariable(name = "id") String id) {
        TruckDriverLicense driverLicense = truckDriverLicenseService.findOne(id);
        TruckDriverLicenseDto dto = new TruckDriverLicenseDto();
        if (driverLicense != null) {
            BeanUtils.copyProperties(driverLicense, dto);
        }
        return new R<>(ResultCode.SUCCESS_CODE, dto);
    }

    /**
     * 统计司机数量
     *
     * @param fleetId 车队id
     * @return 司机数量
     */
    @GetMapping("/count")
    @ApiOperation("统计司机数量")
    public R<Integer> count(@RequestParam(name = "fleetId", required = false) String fleetId) {
        Integer count = truckDriverService.count(fleetId);
        return new R<>("2000", count);
    }

    /**
     * 获取司机分页数据
     *
     * @param page     页码
     * @param pageSize 页尺寸
     * @param fleetId  车队id
     * @return 司机分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取司机分页数据")
    public R<PageResponse<TruckDriverDto>> findByPage(@RequestParam(name = "page") Integer page,
                                                      @RequestParam(name = "pageSize") Integer pageSize,
                                                      @RequestParam(name = "fleetId", required = false) String fleetId) {
        IPage<TruckDriver> truckPage = truckDriverService.findByPage(page, pageSize, fleetId);
        List<TruckDriverDto> dtoList = new ArrayList<>();
        truckPage.getRecords().forEach(pdTruckDriver -> {
            TruckDriverDto dto = new TruckDriverDto();
            BeanUtils.copyProperties(pdTruckDriver, dto);
            dtoList.add(dto);
        });
        return new R<>("2000", PageResponse.<TruckDriverDto>builder().items(dtoList).pagesize(pageSize).page(page).counts(truckPage.getTotal())
                .pages(truckPage.getPages()).build());
    }

    /**
     * 查询全部
     *
     * @param ids
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部时司机信息")
    public R<List<TruckDriverDto>> findAll(@RequestParam(name = "ids", required = false) List<String> ids) {
        LambdaQueryWrapper<TruckDriver> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TruckDriver::getId, ids);
        return new R<>("2000", truckDriverService.list(wrapper).stream().map(pdTruckDriver -> {
            TruckDriverDto dto = new TruckDriverDto();
            BeanUtils.copyProperties(pdTruckDriver, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

}

